
//
//  UIImage+Extension.swift
//  照片选择
//
//  Created by 孙明海 on 16/2/19.
//  Copyright © 2016年 孙明海. All rights reserved.
//

import UIKit

extension UIImage {
    /// 将图片的宽度缩放到指定的宽度
    func scaleToWidth(width: CGFloat) -> UIImage {
        if width > size.width {
            return self
        }
        let height = size.height * width / size.width
        let rect = CGRectMake(0, 0, width, height)
        
        UIGraphicsBeginImageContext(rect.size)
        drawInRect(rect)
        let result = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        
        return result
    }
}
